package sky.wicket.panels;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.domain.Airport;
import sky.wicket.*;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 05.10.13
 * Time: 13:03
 * To change this template use File | Settings | File Templates.
 */
public class AirportPanel extends Panel {

    private boolean isExpandable = false;
    static final Logger log = LoggerFactory.getLogger(AirportPanel.class);

    public AirportPanel(String id) {
        super(id);
        throw new RuntimeException("Mistaken constructor");
    }

    public AirportPanel(String id, IModel<?> model, boolean isExpandable) {
        super(id, model);
        this.isExpandable = isExpandable;
        Airport airport;
        try {
            airport = (Airport) model.getObject();
        } catch (ClassCastException e) {
            log.error("Cast exception. Expected Airport.class but found " + model.getObject().getClass().toString() + ".class");
            throw new RuntimeException(e);
        }
        addElementsToPanel(airport);
    }

    private void addElementsToPanel(final Airport airport) {
        //info about airport
        MyWicketUtils.addBookmarkablePageLinkWithLabelAndParameters(this, "airportNameLink", AirportInfo.class,
                new PageParameters().add("airportName", airport.getName()), "airportNameLabel", airport.getName());
        //edit airport link
        Link editAirportLink = new Link("editAirportLink") {
            @Override
            public void onClick() {
                setResponsePage(new AddUpdateAirport(airport.getName()));
            }
        };
        editAirportLink.add(new Label("editAirportLabel", "[7]"));
        this.add(editAirportLink);
        //remove airport link
        Link removeAirportLink = new Link("removeAirportLink") {
            @Override
            public void onClick() {
                //remove airport
            }
        };
        removeAirportLink.add(new Label("removeAirportLabel", "[x]"));
        this.add(removeAirportLink);
        //add container
        final WebMarkupContainer container = addContainerToList(airport);
        this.add(container);
        //make container Expandable if it necessary
        AjaxFallbackLink expandLink = MyWicketUtils.addExpandLink(container, "expandLink", "expandLabel");
        add(expandLink);
        expandLink.setVisible(isExpandable);
    }

    private WebMarkupContainer addContainerToList(Airport airport) {
        final WebMarkupContainer container = new WebMarkupContainer("airportInfoContainer");
        container.setOutputMarkupPlaceholderTag(true);
        container.setVisible(!isExpandable);
        //add labels to container
        container.add(new Label("countryLabel", "Country: "));
        container.add(new Label("cityLabel", "City: "));
        container.add(new Label("codeLabel", "Code: "));
        //add links
        //country link
        if (airport.getCity()!=null && airport.getCity().getCountry()!=null) {
            MyWicketUtils.addBookmarkablePageLinkWithLabel(container, "countryInfoLink", HomePage.class,
                    "countryInfoLabel", airport.getCity().getCountry().getName());
            //city link
            MyWicketUtils.addBookmarkablePageLinkWithLabelAndParameters(container, "cityInfoLink", CityInfo.class,
                    new PageParameters().add("cityName", airport.getCity().getName()), "cityInfoLabel", airport.getCity().getName());
        }
        //other elements
        container.add(new Label("code", airport.getCode()));
        return container;
    }
}
